<?php

App::uses('AppController', 'Controller');

/**
 * StudyLevels Controller
 *
 * @property StudyLevel $StudyLevel
 */
class StudyLevelsController extends AppController {

    /**
     * admin_index method
     *
     * @return void
     */
    public function admin_index($id = null) {
        $this->StudyLevel->recursive = 0;
        $conditios = array("StudyLevel.subject_id" => $id);
        $this->set('studyLevels', $this->paginate($conditios));
        $this->set("subject_id", $id);
    }

    /**
     * admin_view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        $this->StudyLevel->id = $id;
        if (!$this->StudyLevel->exists()) {
            throw new NotFoundException(__('Invalid data'));
        }
        $this->set('studyLevel', $this->StudyLevel->read(null, $id));
    }

    /**
     * admin_add method
     *
     * @return void
     */
    public function admin_add($id = null) {
        $subjects = $this->StudyLevel->Subject->find('list');
        if (!$id)
            $this->redirect(array("controller" => "subjects", 'action' => 'index'));
        if ($this->request->is('post')) {


            $this->StudyLevel->create();
            if ($this->StudyLevel->save($this->request->data)) {
                $this->Session->setFlash(__('Data has been saved'), 'default', array("class" => "mws-form-message success"));
                $this->redirect(array('action' => 'index', $id));
            } else {
                $this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default', array("class" => "mws-form-message error"));
            }
        }
        $this->request->data["StudyLevel"]["subject_id"] = $id;
        $this->set(compact('subjects'));
    }

    /**
     * admin_edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        $this->StudyLevel->id = $id;
        if (!$this->StudyLevel->exists()) {
            throw new NotFoundException(__('Invalid study level'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {

            if (isset($this->request->data["StudyLevel"]["user_id"])) {
                //$useId =AuthComponent::user('id');		
                $useId = "1";
                $this->request->data["StudyLevel"]["user_id"] = $useId;
            }

            if ($this->StudyLevel->save($this->request->data)) {
                $this->savelog($id);
                $this->Session->setFlash(__('Data has been saved'), 'default', array("class" => "mws-form-message success"));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default', array("class" => "mws-form-message error"));
            }
        } else {
            $this->request->data = $this->StudyLevel->read(null, $id);
        }
        $subjects = $this->StudyLevel->Subject->find('list');
        $this->set(compact('subjects'));
        $this->render("admin_add");
    }

    /**
     * admin_delete method
     *
     * @throws MethodNotAllowedException
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->StudyLevel->id = $id;
        if (!$this->StudyLevel->exists()) {
            throw new NotFoundException(__('Invalid study level'));
        }
        if ($this->StudyLevel->delete()) {
            $this->savelog($id);

            $this->Session->setFlash(__('Data has been deleted'), 'default', array("class" => "mws-form-message success"));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Data was not deleted'), array('action' => 'index'), array('action' => 'index'));
        $this->redirect(array('action' => 'index'));
    }

}
